﻿/*
Copyright © 2011 François Karman

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

See <license.txt> for the complete license of the software.
*/

using System;

namespace Simple.Storage.Api
{
    /// <summary>
    /// Implements the <see cref="IIndexDefinition"/> interface.
    /// </summary>
    /// <typeparam name="T">
    /// The type of the persisted objects.
    /// </typeparam>
    /// <typeparam name="TIndex">
    /// The type of the indexed values.
    /// </typeparam>
    public class DefaultIndexDefinition<T, TIndex> : IIndexDefinition
    {
        /// <summary>
        /// The name of the index.
        /// </summary>
        private readonly string name;

        /// <summary>
        /// The function used to retrieve the index value associated with an object.
        /// </summary>
        private readonly Func<T, TIndex> retrieveIndexValue;

        /// <summary>
        /// Initializes a new instance of the DefaultIndexDefinition class.
        /// </summary>
        /// <param name="name">
        /// The name of the index.
        /// </param>
        /// <param name="retrieveIndexValue">
        /// The function used to retrieve the index value associated with an object.
        /// </param>
        public DefaultIndexDefinition(string name, Func<T, TIndex> retrieveIndexValue)
        {
            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentNullException("name");
            }
            else if (retrieveIndexValue == null)
            {
                throw new ArgumentNullException("retrieveIndexValue");
            }

            this.name = name;
            this.retrieveIndexValue = retrieveIndexValue;
        }

        /// <summary>
        /// Gets the name of the index.
        /// </summary>
        public string Name
        {
            get { return this.name; }
        }

        /// <summary>
        /// Gets the type of the persisted instances.
        /// </summary>
        public Type Type
        {
            get { return typeof(T); }
        }

        /// <summary>
        /// Gets the type of the associated indexed values.
        /// </summary>
        public Type IndexType
        {
            get { return typeof(TIndex); }
        }

        /// <summary>
        /// Retrieves the index of an object associated with this definition.
        /// </summary>
        /// <param name="value">A <see cref="IIndexDefinition.Type"/> instance.</param>
        /// <returns>
        /// The associated identifier  of <see cref="IIndexDefinition.IndexType"/> type.
        /// </returns>
        public object RetrieveIndexValue(object value)
        {
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }
            else if (!(value is T))
            {
                // TODO: message
                throw new NotSupportedException();
            }

            return this.retrieveIndexValue((T)value);
        }
    }
}
